//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.preferences;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.JButton;
import org.jpgolay.wi.persistence.UtilityDB;
import org.jpgolay.wi.propertie.PropertyCRUD;
import org.jpgolay.wi.propertie.PropertyGUI;
import org.jpgolay.wi.util.Parameter;

// Referenced classes of package org.jpgolay.wi.preferences:
//            PreferencesGUI, PreferencesCRUD
public class PreferencesController {

    public PreferencesController(Parameter param) {
        preferencesGUI = null;
        write = false;
        this.param = param;
        initLaw();
        init();
    }

    private void initLaw() {
        write = true;
    }

    private void init() {
        getPreferencesGUI();
    }

    public PreferencesGUI getPreferencesGUI() {
        if (preferencesGUI == null) {
            preferencesGUI = new PreferencesGUI();
            preferencesGUI.setResizable(false);
            preferencesGUI.setIconifiable(false);
            preferencesGUI.setClosable(true);
            preferencesGUI.setMaximizable(false);
            preferencesGUI.getDBButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if (write) {
                        actionDB();
                    }
                }
            });
            preferencesGUI.getOKButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if (write) {
                        actionOK();
                    }
                }
            });
            preferencesGUI.getCANCELButton().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCANCEL();
                }
            });
            preferencesGUI.toFront();
            preferencesGUI.setVisible(true);
            preferencesGUI.setLocation(75, 50);
            preferencesGUI.setSize(328, 155);
            getValues();
        }
        return preferencesGUI;
    }

    private void getValues() {
        PreferencesCRUD crud = new PreferencesCRUD();
        preferencesGUI.setDecimal(Integer.parseInt(crud.getNumberOfDecimal()));
        preferencesGUI.setRound(Integer.parseInt(crud.getRoundType()));
        preferencesGUI.setMargin(crud.getMinimumProfitMargin());
    }

    private void actionOK() {
        PreferencesCRUD crud = new PreferencesCRUD();
        crud.setMinimumProfitMargin(verifyMargin(preferencesGUI.getMargin()));
        crud.setNumberOfDecimal(Integer.toString(preferencesGUI.getDecimal()));
        crud.setRoundType(Integer.toString(preferencesGUI.getRound()));
        crud.update();
        param.setMinimumDecimalNumber(new Integer(crud.getNumberOfDecimal()));
        param.setRoundType(new Integer(crud.getRoundType()));
        param.setMinimumProfitMargin(crud.getMinimumProfitMargin());
        actionCANCEL();
    }

    private String verifyMargin(String margin) {
        try {
            return (new BigDecimal(margin)).setScale(2, 4).toString();
        } catch (NumberFormatException e) {
            return "30.00";
        }
    }

    private void actionCANCEL() {
        preferencesGUI.setVisible(false);
        preferencesGUI = null;
    }

    private void actionDB() {
        dbAccess();
    }

    private void dbAccess() {
        if (propGui == null) {
            propGui = new PropertyGUI();
            propGui.setDefaultCloseOperation(0);
            propGui.getCancelB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    propGui.setVisible(false);
                }
            });
            propGui.getOkB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    propertyUpdate();
                }
            });
            prop = new PropertyCRUD();
        }
        propGui.setAdminPass(prop.getFirstPass());
        propGui.setAdminUrl(prop.getFirstUrl());
        propGui.setAdminUser(prop.getFirstUser());
        propGui.setDbBackup(prop.getBackupHome());
        propGui.setDbDriver(prop.getDriver());
        propGui.setDbHome(prop.getDataBaseHome());
        propGui.setIp(prop.getIP());
        propGui.setSerial(Integer.toString(prop.getSerial()));
        propGui.setWiPass(prop.getPass());
        propGui.setWiUrl(prop.getUrl());
        propGui.setWiUser(prop.getUser());
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        propGui.setBounds((screen.width - 466) / 2, (screen.height - 340) / 2, 466, 340);
        propGui.setVisible(true);
    }

    private void propertyUpdate() {
        UtilityDB uDB = null;
        prop.setFirstPass(propGui.getAdminPass());
        prop.setFirstUrl(propGui.getAdminUrl());
        prop.setFirstUser(propGui.getAdminUser());
        prop.setBackupHome(propGui.getDbBackup());
        prop.setDriver(propGui.getDbDriver());
        prop.setDataBaseHome(propGui.getDbHome());
        prop.setIp(propGui.getIp());
        try {
            prop.setSerial(Integer.parseInt(propGui.getSerial()));
        } catch (NumberFormatException e) {
            prop.setSerial(0);
        }
        prop.setPass(propGui.getWiPass());
        prop.setUrl(propGui.getWiUrl());
        prop.setUser(propGui.getWiUser());
        prop.update();
        if (propGui.isDbRemove()) {
            uDB = new UtilityDB();
            uDB.deleteMysqlDB();
        }
        if (propGui.isDbNew()) {
            if (uDB == null) {
                uDB = new UtilityDB();
            }
            uDB.createMysqlDB();
        }
        propGui.setVisible(false);
        propGui = null;
        prop = null;
    }
    private PreferencesGUI preferencesGUI;
    private Parameter param;
    private PropertyCRUD prop;
    private PropertyGUI propGui;
    private boolean write;
}
